﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace AAA.Base.Util.Cryptography
{
    public class RSA
    {
        public static void CreateKey()
        {
          RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 
         // 公鑰 xml 字串 
          string publicXml = rsa.ToXmlString(false); 
         // 私鑰 xml 字串 
          string privateXml = rsa.ToXmlString(true); 
         // 公鑰 
         // publicParameter = rsa.ExportParameters(false); 
         // 私鑰 
         // privateParameter = rsa.ExportParameters(true); 
        }

        /// <summary> 
        /// RSA 加密字串 
        /// </summary> 
        /// <span  name="original" class="mceItemParam"></span>原始字串</param> 
        /// <span  name="xmlString" class="mceItemParam"></span>公鑰 xml 字串</param> 
        /// <returns></returns> 
        public static string EncryptString(string original, string xmlString)
        {
            try
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlString);
                byte[] s = Encoding.ASCII.GetBytes(original);
                return BitConverter.ToString(rsa.Encrypt(s, false)).Replace("-", string.Empty);
            }
            catch { return original; }
        }

        /// <summary> 
        /// RSA 加密字串 
        /// 加密後為 256 Bytes Hex String (128 Byte) 
        /// </summary> 
        /// <span  name="original" class="mceItemParam"></span>原始字串</param> 
        /// <span  name="parameters" class="mceItemParam"></span>公鑰 RSAParameters 類別</param> 
        /// <returns></returns> 
        public static string EncryptString(string original, RSAParameters parameters)
        {
            try
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.ImportParameters(parameters);
                byte[] s = Encoding.ASCII.GetBytes(original);
                return BitConverter.ToString(rsa.Encrypt(s, false)).Replace("-", string.Empty);
            }
            catch { return original; }
        }

        /// <summary> 
        /// RSA 解密字串 
        /// </summary> 
        /// <span  name="hexString" class="mceItemParam"></span>加密後 Hex String</param> 
        /// <span  name="xmlString" class="mceItemParam"></span>私鑰 xml 字串</param> 
        /// <returns></returns> 
        public static string DecryptString(string hexString, string xmlString)
        {
            try
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlString);
                byte[] s = new byte[hexString.Length / 2];
                int j = 0;
                for (int i = 0; i < hexString.Length / 2; i++)
                {
                    s[i] = Byte.Parse(hexString[j].ToString() + hexString[j + 1].ToString(), System.Globalization.NumberStyles.HexNumber);
                    j += 2;
                }
                return Encoding.ASCII.GetString(rsa.Decrypt(s, false));
            }
            catch { return hexString; }
        }

        /// <summary> 
        /// RSA 解密字串 
        /// </summary> 
        /// <span  name="hexString" class="mceItemParam"></span>加密後 Hex String</param> 
        /// <span  name="parameters" class="mceItemParam"></span>私鑰 RSAParameters 類別</param> 
        /// <returns></returns> 
        public static string DecryptString(string hexString, RSAParameters parameters)
        {
            try
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.ImportParameters(parameters);
                byte[] s = new byte[hexString.Length / 2];
                int j = 0;
                for (int i = 0; i < hexString.Length / 2; i++)
                {
                    s[i] = Byte.Parse(hexString[j].ToString() + hexString[j + 1].ToString(), System.Globalization.NumberStyles.HexNumber);
                    j += 2;
                }
                return Encoding.ASCII.GetString(rsa.Decrypt(s, false));
            }
            catch { return hexString; }
        } 
 

    }
}
